package mo.leetcode_top;

public class L134 {

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int sum = 0;
        int start = 0;
        int total = 0;
        for (int i = 0; i < gas.length; i++) {
            total += gas[i] - cost[i];
            sum += gas[i] - cost[i];
            if (sum < 0) {
                sum = 0;
                start = i+1;
            }
        }
        return total < 0 ? -1 : start;
    }

    public static void main(String[] args) {
        L134 test = new L134();
        System.out.println(test.canCompleteCircuit(new int[]{1,2,3,4,5}, new int[]{3,4,5,1,2}));
    }
}
